/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Servlets;

import BancoDados.Profissao;
import Sistema.GerenciadorServlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created on : May 26, 2011, 9:44:51 PM
 * @author diogo
 */
@WebServlet(name="ServletProfissao", urlPatterns={"/ServletProfissao"})
public class ServletProfissao extends HttpServlet {
   // <editor-fold defaultstate="collapsed" desc="netbeans default template">
////    /**
////     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
////     * @param request servlet request
////     * @param response servlet response
////     * @throws ServletException if a servlet-specific error occurs
////     * @throws IOException if an I/O error occurs
////     */
////    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
////    throws ServletException, IOException {
////        response.setContentType("text/html;charset=UTF-8");
////        PrintWriter out = response.getWriter();
////        try {
////            /* TODO output your page here
////            out.println("<html>");
////            out.println("<head>");
////            out.println("<title>Servlet ServletProfissao</title>");
////            out.println("</head>");
////            out.println("<body>");
////            out.println("<h1>Servlet ServletProfissao at " + request.getContextPath () + "</h1>");
////            out.println("</body>");
////            out.println("</html>");
////            */
////        } finally {
////            out.close();
////        }
////    }
	// </editor-fold>

    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        StringBuffer output = new StringBuffer("");


        response.getWriter().print(output);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        GerenciadorServlet.Codificar(request, response);
		StringBuffer output = new StringBuffer("");
		String tipo = request.getParameter("tipo");

		HttpSession session = request.getSession();
		Object loginSysAdmin = session.getAttribute("operadorsistema");
		boolean isAdmin = loginSysAdmin != null;

		if (tipo.equals("inserir")){
			boolean resultado;
			resultado = Profissao.Inserir(request);
			output.append(resultado);
			response.sendRedirect("CadastroProfissao.jsp");
		} else if (isAdmin && tipo.equals("atualizar")) {
			// apenas administradores podem mudar!
			
			boolean resultado = false;

			String sCodProfissao = request.getParameter("CodProfissao");
			String novaDescricao = request.getParameter("Descricao");
			String sNovaArea = request.getParameter("CodArea");
			try {
				int CodProfissao = Integer.parseInt(sCodProfissao);
				int CodArea = Integer.parseInt(sNovaArea);

				resultado = Profissao.Alterar(CodProfissao, novaDescricao, CodArea);
			} catch (Exception e) {
			}
			
			output.append(resultado);
		} else if (isAdmin && tipo.equals("deletar")) {
			// apenas administradores podem mudar!
			boolean resultado = false;
			try {
				int CodProfissao = Integer.parseInt(request.getParameter("CodProfissao"));
				resultado = Profissao.Deletar(CodProfissao);
			} catch (Exception e) {
			}
			output.append(resultado);
		}
        response.getWriter().print(output);
    }

    // <editor-fold defaultstate="collapsed" desc="netbeans default template">
    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
////    @Override
////    public String getServletInfo() {
////        return "Short description";
////    }
    // </editor-fold>
}
